var G_timer;
var G_context;
var G_canvas;
var G_listeRegion = new Array();
var G_zoom = 1;        // En pourcentage

function drawCarte(){
    G_canvas = document.getElementById("zone");
    G_context = G_canvas.getContext("2d");
    
    G_listeRegion.push(new clsRegion("Bretagne","/pngs/regions/bretagne.png","#FFFFFF",277,109));
    G_listeRegion.push(new clsRegion("Anjou","/pngs/regions/anjou.png","#9999FF",432,125));
    G_listeRegion.push(new clsRegion("Basse Normandie","/pngs/regions/basse_normandie.png","#9999FF",425,79));
    G_listeRegion.push(new clsRegion("Haute Normandie","/pngs/regions/haute_normandie.png","#ff00ff",513,86));

    G_listeRegion.push(new clsRegion("Touraine","/pngs/regions/touraine.png","#9999FF",492,154));
    G_listeRegion.push(new clsRegion("Pas de Calais","/pngs/regions/pas_de_calais.png","#9999FF",592,65));
    G_listeRegion.push(new clsRegion("Flandres","/pngs/regions/flandres.png","#ffff00",703,46));
    G_listeRegion.push(new clsRegion("Artois","/pngs/regions/artois.png","#9999FF",615,81));
    G_listeRegion.push(new clsRegion("Ile de France","/pngs/regions/ile_de_france.png","#9999FF",617,116));
    G_listeRegion.push(new clsRegion("Orleannais","/pngs/regions/orleannais.png","#9999FF",543,139));

    G_listeRegion.push(new clsRegion("Auxerois","/pngs/regions/auxerois.png","#9999FF",638,157));
    G_listeRegion.push(new clsRegion("Champagne","/pngs/regions/champagne.png","#9999FF",699,110));
    G_listeRegion.push(new clsRegion("Ardennes","/pngs/regions/ardennes.png","#9999FF",809,74));
    G_listeRegion.push(new clsRegion("Duché de Bourgogne","/pngs/regions/duche_de_bourgogne.png","#FF0000",733,199));
    G_listeRegion.push(new clsRegion("Compté de Bourgogne","/pngs/regions/compte_de_bourgogne.png","#FF0000",831,141));

    G_listeRegion.push(new clsRegion("Navarre","/pngs/regions/navarre.png","#f8bb00",0,364));
    G_listeRegion.push(new clsRegion("Languedoc","/pngs/regions/languedoc.png","#9999FF",473,344));
    
    //clearInterval(G_timer);
    //G_timer = setInterval('gestion()',100);    
}

//function gestion(){
//    // En attendant mieux...
//    for(n=0;n<G_listeRegion.length;n++) {
//        if (G_listeRegion[n].ready) {
//            G_listeRegion[n].draw(G_context);
//            G_listeRegion[n].ready = false;
//        }
//    }
//}

function testXML(){
    var sx = null;
    
    try {
        sx = new XMLrequest();
        //sx.ajouteParam("nom","bob");
        //sx.ajouteParam("royaume","France");
        //sx.ajouteParam("titre","Duc");
        //sx.ajouteParam("argent","10000");
        //sx.ajouteParam("insurge","0");
        //sx.envois("/main/test");
        //sx.recupere("/main/connexion");
        sx.ajouteParam("clePartie","agVnYW1lc3IOCxIIZGJQYXJ0aWUYMAw");
        sx.recupere("/main/recupereRegion?");
        alert(sx.resultatXML());
    }
    catch(e){
        alert(e.message);
    }
}

function testMouse(e){
    var xMouse, yMouse;

    if(e.offsetX) {
        xMouse = e.offsetX;
        yMouse = e.offsetY;
    }
    else if(e.layerX) {
        xMouse = e.layerX;
        yMouse = e.layerY;
    }

    nom = "";
    for(n=0;n<G_listeRegion.length;n++)
        if (G_listeRegion[n].testCollisionXY(xMouse,yMouse)) nom = G_listeRegion[n].nom;
    
    if (nom.length>0){
        document.getElementById("region").innerHTML = "<div>"+nom+"<div/>";
        document.body.style.cursor="pointer";
    } else {
        document.getElementById("region").innerHTML = "";
        document.body.style.cursor="default"; // "wait";
    }
    
}

function clsRegion(nom,source,couleur,x,y){
    Sprite.call(this);    // On dérive sprite
    // Le tricks de la mort reste super important
    var _this = this;

    this.nom = nom;       // On met les données particulière en place
    this.x = x;
    this.y = y;
    this.couleur = couleur;

    var teinte = function(){
        G_context.save();
        var buffer = document.createElement('canvas');
        buffer.width = _this.width * G_zoom;
        buffer.height = _this.height * G_zoom;
        
        var bx = buffer.getContext('2d');
        bx.fillStyle = _this.couleur;
        bx.fillRect(0,0,buffer.width,buffer.height);
        bx.globalCompositeOperation = "destination-atop";
        _this.draw(bx,G_zoom,true);
        
        G_context.globalAlpha = .4;
        G_context.drawImage(buffer,_this.left*G_zoom,_this.top*G_zoom);
        G_context.restore();
        buffer=null;
    }
    
    this.load(source,C_TOPLEFT,teinte);    // On charge l'image
}
